// 集中封装一些通用的方法函数

//将路由菜单数据包，处理为键值对格式【path---title】
import { children } from "@/router";
export const routeMapTool = () => {
  let routeMap = {};
  function loop(arr) {
    arr.forEach((item) => {
      routeMap[item.path] = item.meta.title;
      if (item.children) {
        loop(item.children);
      }
    });
  }
  loop(children);
  return routeMap;
};

//根据角色权限，过滤菜单数据
// 已知条件：路由数据包、permission
import store from "@/store";
export const filterMenu = () => {
  let { permission } = store.state.user.userInfo.roleData;
  let menu = JSON.parse(JSON.stringify(children));
  function loop(arr) {
    for (let i = arr.length - 1; i >= 0; i--) {
      // '/category'   ['/category/list']   代入特殊值理解逻辑
      let isLike = permission.filter((item) => item.indexOf(arr[i].path) != -1);
      console.log(isLike);
      if (!permission.includes(arr[i].path) && isLike.length == 0) {
        arr.splice(i, 1);
      }
      if (arr[i] && arr[i].children) {
        loop(arr[i].children);
      }
    }
  }
  loop(menu);
  return menu;
};
